import { NDialog, type DialogProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type DialogFactoryConstructorType = {
  props?: DialogProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  headerSlot?: BaseComponentType;
  iconSlot?: BaseComponentType;
  actionSlot?: BaseComponentType;
  closeSlot?: BaseComponentType;
};

export class DialogFactory {
  private attrs: HTMLAttributes = {};
  private props: DialogProps = {};
  private defaultSlot: BaseComponentType = null;
  private headerSlot: BaseComponentType = null;
  private iconSlot: BaseComponentType = null;
  private actionSlot: BaseComponentType = null;
  private closeSlot: BaseComponentType = null;
  constructor(param?: DialogFactoryConstructorType) {
    if (param?.props) this.setProps(param.props);
    if (param?.attrs) this.setAttrs(param.attrs);
    if (param?.defaultSlot) this.setDefault(param.defaultSlot);
    if (param?.headerSlot) this.setHeader(param.headerSlot);
    if (param?.iconSlot) this.setIcon(param.iconSlot);
    if (param?.actionSlot) this.setAction(param.actionSlot);
    if (param?.closeSlot) this.setClose(param.closeSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: DialogProps) {
    this.props = props;
    return this;
  }
  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }
  setHeader(component: BaseComponentType) {
    this.headerSlot = component;
    return this;
  }
  setIcon(component: BaseComponentType) {
    this.iconSlot = component;
    return this;
  }
  setAction(component: BaseComponentType) {
    this.actionSlot = component;
    return this;
  }
  setClose(component: BaseComponentType) {
    this.closeSlot = component;
    return this;
  }

  create() {
    return (
      <NDialog {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,
          header:
            this.headerSlot != null ? () => getSlotsDom(this.headerSlot) : null,
          icon: this.iconSlot != null ? () => getSlotsDom(this.iconSlot) : null,
          action:
            this.actionSlot != null ? () => getSlotsDom(this.actionSlot) : null,
          close:
            this.closeSlot != null ? () => getSlotsDom(this.closeSlot) : null,
        }}
      </NDialog>
    );
  }
}
